package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://www.zgfznews.com/ 抚州日报
 * 
 * @author Chase  E-mail:594126573@qq.com
 *
 * @version 创建时间：2012-12-4  下午01:57:03
 */
public class ZgfznewsNewsPaperAnalyse implements AnalyseNews {

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
						
		newspaper.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Document doc = Jsoup.parse(html);
		
		title =doc.select("title").text();
		String dateStr = null;
		dateStr = doc.select("html body center table tbody tr td.content2").text();
		date=DateUtils.matchDate(dateStr);
		content =HtmlCleaner.getContentHtml(url,doc.select("td.content"));
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		/*
		 * http://www.zgfznews.com/epaper/fzdaily/1b/2012/11/30/2012/11/30/957018.shtml
		 * http://www.zgfznews.com/epaper/fzdaily/1b/2012/12/4/2012/12/4/958249.shtml
		 *
		 */
		String[] regex = {
				"http://www.zgfznews.com/epaper/fzdaily/.*/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9]*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		/*
		 * http://www.zgfznews.com/fzrbpaper/2502_11469.shtml
		 */
		String[] regex = {
				"http://www.zgfznews.com/fzrbpaper/[0-9]*_[0-9]*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
}
